﻿using System.Collections;
using System.Collections.Generic;
using Microsoft.FxCop.Sdk;

namespace LINQtoFxCop.Core
{
  public abstract class MemberCollection<T> : ICollection<T> where T : Node
  {
    private readonly IDictionary<int, T> _nodes;

    protected MemberCollection()
    {
      _nodes = new Dictionary<int, T>();
    }

    public T this[T item]
    {
      get
      {
        if (_nodes.ContainsKey(item.UniqueKey))
          return _nodes[item.UniqueKey];
        return null;
      }
    }

    public IEnumerator<T> GetEnumerator()
    {
      return _nodes.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return GetEnumerator();
    }

    public void Add(T item)
    {
      _nodes.Add(item.UniqueKey, item);
    }

    public void Clear()
    {
      _nodes.Clear();
    }

    public bool Contains(T item)
    {
      return _nodes.ContainsKey(item.UniqueKey);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
      throw new System.NotImplementedException();
    }

    public bool Remove(T item)
    {
      return _nodes.Remove(item.UniqueKey);
    }

    public int Count
    {
      get { return _nodes.Count; }
    }

    public bool IsReadOnly
    {
      get { return _nodes.IsReadOnly; }
    }
  }
}
